﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6_05_多窗体实现数据增删改查_窗体传值_
{
    public partial class EditFrm : Form
    {
        public EditFrm()
        {
            InitializeComponent();
        }
        public int MenId { get; set; }
        public EditFrm(int MenId)
        {
            InitializeComponent();
            this.MenId = MenId;
          
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlConnection = new SqlConnection(conn);
            sqlConnection.Open();
            //string sql = $"update Member set MemberAccount ='{this.textBox1.Text}'," +
            //    $" MemberPwd='{this.textBox2.Text}',MemberName='{this.textBox3.Text}',MemberPhone='{this.textBox4.Text}'" +
            //    $"where MemberId={this.MenId}";
            string sql = $"update Member set MemberAccount =@MemberAccount,MemberPwd=@MemberPwd,MemberName=@MemberName," +
                $"MemberPhone=@MemberPhone where MemberId=@MemberId";
            SqlCommand sqlcmd = new SqlCommand(sql, sqlConnection);
            sqlcmd.Parameters.Add(new SqlParameter("MemberAccount", this.textBox1.Text));
            sqlcmd.Parameters.Add(new SqlParameter("MemberPwd", this.textBox2.Text));
            sqlcmd.Parameters.Add(new SqlParameter("MemberName", this.textBox3.Text));
            sqlcmd.Parameters.Add(new SqlParameter("MemberPhone", this.textBox4.Text));
            sqlcmd.Parameters.Add(new SqlParameter("MemberId", this.MenId));
            if (sqlcmd.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("更新成功");
                this.textBox1.Text = "";
                this.textBox2.Text = "";
                this.textBox3.Text = "";
                this.textBox4.Text = "";
                //强转识别类型 得到可执行函数
                ((Form1)(this.Owner)).BindData();
                this.Close();
            }
            else
            {
                MessageBox.Show("更新失败");
            }
            sqlConnection.Close();
        }

        private void EditFrm_Load(object sender, EventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlConnection = new SqlConnection(conn);
            sqlConnection.Open();
            string sql = "select * from Member where MemberId=" + MenId;
            SqlDataAdapter adp = new SqlDataAdapter(sql, sqlConnection);
            DataTable dt = new DataTable();//查询的数据存入内存
            adp.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("查询失败");
                return;
            }
            this.textBox1.Text = dt.Rows[0]["MemberAccount"].ToString();
            this.textBox2.Text = dt.Rows[0][1].ToString();
            this.textBox3.Text = dt.Rows[0][2].ToString();
            this.textBox4.Text = dt.Rows[0][3].ToString();
            sqlConnection.Close();
        }
    }
}
